namespace Xmef.Engine.Internal
{
    using System;
    using System.IO;
    using System.Reflection;
    using Xmef.Interfaces;
    using Xmef.Library;

    [Serializable]
    internal class PluginResourceContainer : PluginIdentity, IPluginResourceContainerInternal, IPluginIdentity
    {
        private readonly Assembly _assembly;

        public PluginResourceContainer()
        {
        }

        public PluginResourceContainer(Assembly assembly)
        {
            this._assembly = assembly;
        }

        public byte[] GetManifestResourceData(string name)
        {
            Stream manifestResourceStream = this._assembly.GetManifestResourceStream(name);
            if (manifestResourceStream != null)
            {
                byte[] buffer = new byte[(int) manifestResourceStream.Length];
                if (manifestResourceStream.Read(buffer, 0, buffer.Length) != buffer.Length)
                {
                    throw new InvalidDataException("Resource stream error");
                }
                return buffer;
            }
            return null;
        }

        public ManifestResourceInfo GetManifestResourceInfo(string name)
        {
            return this._assembly.GetManifestResourceInfo(name);
        }

        public string[] GetManifestResourceNames()
        {
            return this._assembly.GetManifestResourceNames();
        }

        public Stream GetManifestResourceStream(string name)
        {
            return new MemoryStream(this.GetManifestResourceData(name));
        }
    }
}

